<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="学生名称" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="studentMobile">
        <el-input
          v-model="queryParams.studentMobile"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="学生状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择学生状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_student_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['school:student:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['school:student:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['school:student:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['school:student:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="studentCode" />
      <el-table-column label="学员姓名" align="center" prop="studentName" />
      <el-table-column label="学生性别" align="center" prop="studentSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.studentSex" />
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="studentBirthday">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.studentBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="在读学校" align="center" prop="atSchool" width="200" show-overflow-tooltip /> -->
      <el-table-column label="家长姓名" align="center" prop="parentName" />
      <el-table-column label="联系电话" align="center" prop="studentMobile" />
      <!-- <el-table-column label="家庭地址" align="center" prop="studentAddress" show-overflow-tooltip /> -->
      <!-- <el-table-column label="QQ或微信" align="center" prop="studentNumber" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
       <el-table-column label="学生状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_type" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" width="200">
        <template slot-scope="scope">
          <!-- v-hasPermi="['school:student:detail']" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="toDetail(scope.row)"
          >详情</el-button>
          <el-button
            v-hasPermi="['school:student:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['school:student:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <studentModel ref="StudentModel" @updatedata="updateStudentData" />
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, exportStudent } from '@/api/school/student'
import studentModel from '../model/studentModel.vue'

export default {
  name: 'Student',
  dicts: ['sys_user_sex','sys_student_type'],
  components: {
    studentModel
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      studentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        studentMobile: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    updateStudentData() {
      this.getList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      console.log(this.ids)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.StudentModel.reset()
      this.$refs.StudentModel.open = true
      this.$refs.StudentModel.title = '添加学生信息'
      this.$refs.StudentModel.getListClass()
    },
    // 查看详情
    toDetail(row) {
      console.log(row.id)
      this.$router.push({path: `school/student/detail?id=${row.id}`})
    }, 
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.StudentModel.reset()
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.$refs.StudentModel.form = response.data
        this.$refs.StudentModel.open = true
        this.$refs.StudentModel.title = '修改学生信息'
        this.$refs.StudentModel.getListClass()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除所选学生信息？').then(function() {
        return delStudent(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // ids 选择列表数据
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有学生信息数据项？').then(() => {
        this.exportLoading = true
        return exportStudent(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {})
    }
  }
}
</script>
